<?php
require_once PATH_PLUGINS.DS.'smarty'.DS.'libs'.DS.'Smarty.class.php';

/*theme*/
define('_FRONT_THEME','default');
//define('_ADMIN_THEME','default');
define('_ADMIN_THEME','adminskin2');

/**
 * 模板类
 */
class Template extends Smarty{
    
    private $tpl_list;
    private $menu;
    /**
     * setup template path (判断前后台)
     */
    public function __construct($switch){
    	if($switch == 'admin'){
	        $this->template_dir	= PATH_ADMIN_THEMES.DS._ADMIN_THEME;
	        $this->compile_dir  = PATH_ADMIN_DATA.DS._ADMIN_THEME.DS.'template';
	        $this->cache_dir	= PATH_ADMIN_DATA.DS._ADMIN_THEME.DS.'cache';
	        $this->config_dir	= PATH_ADMIN_DATA.DS._ADMIN_THEME.DS.'config';
	        $this->base_path = 'http://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/')).'/themes/'._ADMIN_THEME;
    	}elseif($switch == 'font'){
	        $this->template_dir	= PATH_THEMES.DS._FRONT_THEME;
	        $this->compile_dir  = PATH_DATA.DS._FRONT_THEME.DS.'template';
	        $this->cache_dir	= PATH_DATA.DS._FRONT_THEME.DS.'cache';
	        $this->config_dir	= PATH_DATA.DS._FRONT_THEME.DS.'config';
	        $this->base_path = 'http://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/')).'/themes/'._FRONT_THEME;
    	}else{
    		$err = '模板路径设置错误';
    		return $err;
    	}
    	
	    /* 预定义变量 */
        $this->assign('base_path',$this->base_path);
        //echo $this->base_path;
    	
    }
    
    /**
     * ($page,$default) (跳转页,默认页)
     * display template page
     */
    public function page($page,$default){
    	$r = $this->getPage($page);
        if($r){
            $this->setPage($r);
        }else{
            $this->setPage($default);
        }
    }
    
    /**
     * ($page)
     * check the template page exist or not
     */
    protected function getPage($page){
        if(isset($page) && $page){
        	//检测文件是否存在
        	if(is_file($this->template_dir.DS.$page.".tpl")){
            	return $page;
        	}else{
        		debug('Err: 404 , page not exist!');
        		return false;
        	}
        }else{
            return false;
        }
    }
    /**
     * ($_page)
     * set the template page and display 
     */
    protected function setPage($page){
    	$this->assign('active',$page);
    	$this->display($page.'.tpl');        
    }
    
    /**
     * ($_theme,[$option])
     * setup the theme
     */
    public function theme(){
        $this->getTheme();
        $this->setTheme();
    }
    protected function getTheme(){
        
    }
    protected function setTheme(){
        
    }
    public function themePages(){
        
    }
    /**
     * 后台子菜单
     */    
    public function subMenu($sub,$active,$tpl){
    	$this->menu = $GLOBALS['menu'][$tpl];
    	
    	$r = $this->getSubMenu($sub,$tpl);
        if($r){
            $this->setSubMenu($r);
        }else{
            $this->setSubMenu($active);
        }
    }
    /**
     * 获取子菜单
     */
    public function getSubMenu($sub,$tpl){
        if(isset($sub) && $sub){
        	//debug($GLOBALS['menu'][$tpl]);
        	if(in_array($sub,$this->menu)){
            	return $sub;
        	}else{
        		debug('Err: Can\'t find SubMenu!');
        		return false;
        	}
        }else{
            return false;
        }
    }
    /**
     * 设置子菜单
     */
    public function setSubMenu($sub){
    	$this->assign('active_sub_menu',$sub);
    	$this->assign('sub_menu',$this->menu);
    }
    
}
?>